﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;

namespace PayRoll
{
    public partial class Login : Form
    {
          
        private string ErrMsg = "Please enter the correct Username / Password combination!";
        private string ErrMsgAccountLocked = "Account is locked out.Contact Administrator!";
        private bool bAccountLocked = false;
        private bool bIsPasswordChanged = false;
        private int intEmployeeID = 0;

           
        public Login()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {





            //Report frmChangePassword = new Report();
            //frmChangePassword.PerEmployeePaymentReport(54, DateTime.Now.AddDays(-5), DateTime.Now.AddDays(5));
            //frmChangePassword.Show();


            //return;



            try
            {
                bool blStatus = CheckCredentials(txtUserName.Text.Trim().ToUpper(), txtPassword.Text.Trim());

                if (blStatus)
                {
                    Utility.LoginEmployeeID = intEmployeeID;
                    //Check whether user has changed passoword
                    //if (!bIsPasswordChanged)
                    //{
                    //    this.Hide();
                    //    ChangePassword ObjChangePassword = new ChangePassword(intEmployeeID);
                    //    ObjChangePassword.Show();
                    //}
                    //else
                    //{
                        this.Hide();
                        StartMain main = new StartMain(intEmployeeID, bIsPasswordChanged);
                        main.Show();
                  //  }
                }
                else
                {
                    if (bAccountLocked)
                        MessageBox.Show(ErrMsgAccountLocked, "Error Message!");
                    else MessageBox.Show(ErrMsg,"Error Message!");
                   

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            
        }               
               
        private bool CheckCredentials(string usr, string pwd)
        {
            ClsLogin objClsLogin = new  ClsLogin();
            objClsLogin.GetUserCredentials(usr);

            //Check whether User account is locked out
            if (objClsLogin.FailedLoginAttempt == 3)
            {
                bAccountLocked = true;
                return false;
            }

            else
            {
                if (objClsLogin.LoginID == usr)
                {
                    if (objClsLogin.Password == pwd)
                    {
                        //Update Last Login date and Time
                        objClsLogin.SaveLastLoginTime(objClsLogin.EmployeeID);
                        bIsPasswordChanged = objClsLogin.IsPasswordChanged;
                        intEmployeeID = objClsLogin.EmployeeID;
                        return true;
                    }
                    else
                    {
                        //Update No of Failed Login Attempt
                        objClsLogin.UpdateFailedLoginAttempt(objClsLogin.EmployeeID);
                        errorProvider1.SetError(btnOk, ErrMsg);
                        return false;
                    }
                }
                else
                {
                    errorProvider1.SetError(btnOk, ErrMsg);
                    return false;
                }
            }
            
        }
     

    }
}
